package com.example.controller;

import com.example.exception.MyException;
import com.example.pojo.Pub;
import com.example.pojo.Query;
import com.example.service.PubManageService;
import com.example.utils.FileUploadUtil;
import com.example.utils.Page;
import com.example.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin//允许跨域
@RestController
@RequestMapping("/pubManage")
@Api(tags = "酒店管理")
public class PubManageController {
    @Autowired
    private PubManageService pubManageService;
    @Autowired
    FileUploadUtil fileUploadUtil;

    @GetMapping("/selectAllPubManage")
    public Result selectAllPubManage(Query query){//条件+分页查询酒店信息
        Page<Pub> page=new Page<>(query.getPageNum(),query.getPageSize(),pubManageService.count(query.getName()));
        page.setList(pubManageService.selectAllPubManage(query.getName(),page.getStart(),query.getPageSize()));
        Map<String,Object> map=new HashMap<>();
        map.put("list", page.getList());
        map.put("count", page.getTotalCount());//总记录数
        map.put("pageSize", query.getPageSize());//每页记录数
        map.put("pageNum", query.getPageNum());//当前页码
        return new Result<>("200", "查询成功", map);
    }

    @PostMapping("/addPubManage")
    public Result addPubManage(@RequestBody Pub pub) {//添加酒店信息
        int i=pubManageService.addPubManage(pub);
        Map<String,Object> map=new HashMap<>();
        if(i>0){
            map.put("code",200);
            map.put("data", pub);
            map.put("msg", "添加成功");
        }else {
            map.put("code",444);
            map.put("data", null);
            map.put("msg", "添加失败");
        }
        return new Result<>("222", "调用成功", map);
    }

    @GetMapping("/deletePubManage")
    public Result deletePubManage(@RequestParam("id") Integer id) {//删除酒店信息
        int i = pubManageService.deletePubManage(id);
        Map<String, Object> map = new HashMap<>();
        if (i > 0) {
            map.put("code", 200);
            map.put("data", null);
            map.put("msg", "删除成功");
        }else {
            map.put("code", 444);
            map.put("data", null);
            map.put("msg", "删除失败");
        }
        return new Result<>("222", "调用成功", map);
    }

    @GetMapping("selectPubManageById")
    public Result selectPubManageById(@RequestParam("id") Integer id) {//根据id查询酒店信息
        Pub pub = pubManageService.selectPubManageById(id);
        Map<String, Object> map = new HashMap<>();
        if (pub != null) {
            map.put("code", 200);
            map.put("data", pub);
            map.put("msg", "查询成功");
        } else {
            map.put("code", 444);
            map.put("data", null);
            map.put("msg", "查询失败");
        }
        return new Result<>("222", "调用成功", map);
    }

    @PostMapping("updatePubManage")
    public Result updatePubManage(@RequestBody Pub pub) {//修改酒店信息
        int i = pubManageService.updatePubManage(pub);
        Map<String, Object> map = new HashMap<>();
        if (i > 0) {
            map.put("code", 200);
            map.put("data", null);
            map.put("msg", "修改成功");
        }else {
            map.put("code", 444);
            map.put("data", null);
            map.put("msg", "修改失败");
        }
        return new Result<>("222", "调用成功", map);
    }

    //上传图片
    @PostMapping("/upload-img")
    public Result uploadImg(@RequestParam("file") MultipartFile file){
        InputStream inputStream = null;
        try{
            inputStream = file.getInputStream();
        }catch (IOException e){
            throw new MyException("606","上传时发生异常");
        }
        String url = fileUploadUtil.upload(inputStream,file.getOriginalFilename());
        return new Result("200","上传成功",url);
    }
}
